

class AddressBook

  @contacts
  @numbers_qlist

  @parent
  
  @list_numbers
  @contact_array
  
  def initialize(parent)
    @parent = parent
    
    config = @parent.get_config()

    yaml = config.readEntry("contacts_new", {}.to_yaml)
    @contacts = YAML.load(yaml)
    SmsWidget::Main.logger.info "contacts yaml inspect: #{@contacts.inspect}"
    @contact_array = []

  end

  def setup_ui()
     
     widget = Qt::Widget.new
     layout = Qt::VBoxLayout.new widget

      label_login = Qt::Label.new widget
      label_login.text = 'Address Book'
      layout.addWidget label_login
      
      layout_input = Qt::HBoxLayout.new
      
      input_name = KDE::LineEdit.new widget
      input_name.setClickMessage("name")
      input_name.setText("name")
      input_name.selectAll()
      #input_name.setStatusTip("contact name")
      #input_name.setAccessibleName("contact name")
      layout_input.addWidget input_name
      
      input_number = KDE::LineEdit.new widget
      #input_number.setText("number")
      input_number.setClickMessage("number")
      #input_number.setStatusTip("number")
      #input_number.setAccessibleName("blah")
      layout_input.addWidget input_number
      
      button_add = Qt::PushButton.new widget
      button_add.text = "Add"
      layout_input.addWidget button_add
      
      layout.addLayout layout_input
      
      @list_numbers = Qt::ListWidget.new widget
      layout.addWidget @list_numbers
      self.load_numbers()

      layout_buttons = Qt::HBoxLayout.new
      
      button_delete = Qt::PushButton.new widget
      button_delete.text = "Delete selected"
      layout_buttons.addWidget button_delete

      button_close = Qt::PushButton.new widget
      button_close.text = "Close"
      layout_buttons.addWidget button_close

      layout.addLayout layout_buttons
      
      button_add.connect(SIGNAL(:clicked)) do
        self.add_number(input_name.text(), input_number.text())
      end
      
      button_delete.connect(SIGNAL(:clicked)) do
        row = @list_numbers.currentRow
        unless row == -1
          self.delete_number(row)
        end
      end

      button_close.connect(SIGNAL(:clicked)) do
        widget.close
      end

      widget.show()
  end
  
  def load_numbers()
    @list_numbers.clear()
    @contact_array.clear
    contacts = self.get_contacts()
    contacts.each_pair do |name, number|
      @list_numbers.addItem("#{number} (#{name})")
      @contact_array.push(name, number)
    end
    @contact_array.inspect
  end
  
  def add_number(name, number)
    contacts = self.get_contacts()
    unless contacts.value?(number)
      @list_numbers.addItem("#{number} (#{name})")
      contacts[name] = number
      self.save_config()
    end
    self.load_numbers
  end
  
  def delete_number(index)
    actual_index = index*2
    key = @contact_array[actual_index]
    contacts = self.get_contacts()
    contacts.delete(key)
    SmsWidget::Main.logger.info contacts.inspect
=begin
    puts "Number to delete: #{index}"
    actual_index = 2*index
    puts "actual_index: #{actual_index}"
    actual_index2 = actual_index + 1
    puts "actual_index+1: #{actual_index2}"
    @contacts.delete_at(actual_index)
    puts @contacts.inspect
    @contacts.delete_at(actual_index)
    puts @contacts.inspect
=end
    self.save_config
    self.load_numbers
  end
  
  def get_contacts()
    @contacts
  end

  def get_contact_numbers()
    @contacts.values
  end

  def get_contact_names()
    @contacts.keys
  end
  
  def save_config()
    config = @parent.get_config()
    
    config.writeEntry("contacts_new", self.get_contacts().to_yaml)
    config.sync()
    #self.get_main.reload_number_input_hints
    SmsWidget::Main.logger.info "Addressbook saved."
    
  end

end
